package com.example.commuity.model

import com.blankj.utilcode.util.GsonUtils
import com.example.commuity.api.CommunityApiServer
import com.example.commuity.entity.AutoPlayFollowCardEntity
import com.example.commuity.entity.CommunityColumnsCardEntity
import com.example.commuity.entity.HorizontalScrollCardEntity
import com.example.entity.NetDataEntity
import com.example.network.ApiUrl
import com.example.network.BaseResponse
import com.google.gson.JsonObject
import okhttp3.ResponseBody
import org.json.JSONObject
import javax.inject.Inject

class CommunityRepository @Inject constructor(private val apiServer: CommunityApiServer){

    /**
     * 获取社区推荐列表
     * */
    suspend fun getRecommendList(url: String =ApiUrl.COMMUNITY_RECOMMEND):BaseResponse{
        var responseBody = apiServer.getCommunityRec(url)
        var resultJsonObj = JSONObject(responseBody.string())
        var itemList = mutableListOf<NetDataEntity>()
        resultJsonObj.optJSONArray("itemList").let {
            for (i in 0 until it.length()){
                var itemJsonObj = it[i] as JSONObject
                var type=itemJsonObj.optString("type")

                when(type){
                    "horizontalScrollCard" -> {
                        var dataType = itemJsonObj.optJSONObject("data").optString("dataType")
                        when(dataType){
                            "ItemCollection" -> itemList.add(NetDataEntity(type, GsonUtils.fromJson(itemJsonObj.toString(), HorizontalScrollCardEntity::class.java)))
                            "HorizontalScrollCard" -> {}
                        }
                    }
                    "communityColumnsCard" -> itemList.add(NetDataEntity(type, GsonUtils.fromJson(itemJsonObj.toString(), CommunityColumnsCardEntity::class.java)))
                }
            }
        }


        return BaseResponse(
            count = resultJsonObj.optInt("count"),
            total = resultJsonObj.optInt("total"),
            itemMap = itemList,
            nextPageUrl = resultJsonObj.optString("nextPageUrl"),
            adExist = resultJsonObj.optBoolean("adExist")
        )
    }

    /**
     * 获取社区关注列表数据
     * */
    suspend fun getAttentionList(url: String=ApiUrl.COMMUNITY_ATTENTION):BaseResponse{

        var responseBody =apiServer.getCommunityAttention(url)
        var resultJsonObj =JSONObject(responseBody.string())
        var itemList = mutableListOf<NetDataEntity>()
        resultJsonObj.optJSONArray("itemList").let {
            for (i in 0 until it.length()){
                var itemJsonObj = it[i] as JSONObject
                var type=itemJsonObj.optString("type")
                when(type){
                    "autoPlayFollowCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),AutoPlayFollowCardEntity::class.java)))
                }
            }
        }


        return BaseResponse(
            count = resultJsonObj.optInt("count"),
            total = resultJsonObj.optInt("total"),
            itemMap = itemList,
            nextPageUrl = resultJsonObj.optString("nextPageUrl"),
            adExist = resultJsonObj.optBoolean("adExist")
        )
    }
}